package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class WmSensitiveServiceImpl implements WmSensitiveService {

    @Autowired
    private WmSensitiveMapper wmSensitiveMapper;

    @Override
    public ResponseResult insert(WmSensitive wmSensitive) {

        // 1. 入参判空
        if (wmSensitive == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        String sensitives = wmSensitive.getSensitives();
        if (StringUtils.isBlank(sensitives)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        // 2. 判断数据库中是否存在该敏感词
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmSensitive::getSensitives, sensitives);
        Integer countResult = wmSensitiveMapper.selectCount(wrapper);

        if (countResult > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }

        // 3.封装数据，插入数据库
        wmSensitive.setCreatedTime(new Date());
        int insertResult = wmSensitiveMapper.insert(wmSensitive);

        // 4.返回结果
        return insertResult > 0 ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public PageResponseResult list(SensitiveDto dto) {

        // 1. 入参判空
        if (dto == null) {
            return new PageResponseResult(0, 0, 0);
        }

        // 1-1 设置页码条数默认状态
        String name = dto.getName();
        Integer page = dto.getPage();
        Integer size = dto.getSize();

        page = page == null ? 1 : page;
        size = size == null ? 10 : size;

        // 2. 设置分页
        Page<WmSensitive> pageResult = new Page<>(page, size);

        // 3. 创建查询条件
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name), WmSensitive::getSensitives, name);
        wrapper.orderByDesc(WmSensitive::getCreatedTime);

        // 3-1 执行查询条件
        Page<WmSensitive> sensitivePage = wmSensitiveMapper.selectPage(pageResult, wrapper);

        // 3-2 获取总条数
        Long total = sensitivePage.getTotal();

        PageResponseResult pageResponseResult = new PageResponseResult(page, size, total.intValue());

        // 4. 设置date参数赋值
        pageResponseResult.setData(sensitivePage.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult update(WmSensitive wmSensitive) {

        // 1. 入参判空
        if (wmSensitive == null || wmSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        // 2. 修改数据
        wmSensitiveMapper.updateById(wmSensitive);

        // 3. 返回修改结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {

        // 1. 入参判空
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        // 2. 删除数据
        wmSensitiveMapper.deleteById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
